/**
 * Created by nico on 2016/1/24.
 */
var AV = require('leanengine');
var async = require('async');
require('date-utils');
var signDayM = require('./signday.js');
var singDays = require('signfunc');

module.exports = {
    getGifts:function(level,uid,action){
        var promise = new AV.Promise();
        async.auto({
            //判断是否领取过
            isGiven:function(callback){
                signDayM.isRecievedGifts(uid,level).then(function(data){
                    console.log('====getGift====isGiven========');
                    if(data.code === 2 ){
                        //未领取过
                        callback(null,{code:0});
                    }else{
                        //已领取过，不发放
                        return callback({code:9999});
                    }
                });
            },
            giveGift:['isGiven',function(callback,results){
                if(results.isGiven.code != 0){
                    console.log('====getGift====giveGift========');
                    return callback({code:9999});
                }
                if(parseInt(action.limitMoney)!=-1){
                    //满减，获取cutMoney，执行满减方法
                    var limitMoney = parseInt(action.limitMoney) ;
                    var cutMoney = parseInt(action.cutMoney);
                    signDayM.giveMJ(uid,limitMoney,cutMoney).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.realMoney) != -1){
                    var realMoney = parseInt(action.realMoney);
                    //执行优惠劵
                    signDayM.giveCoupons(uid,realMoney).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.point) != -1){
                    var point = parseInt(action.point);
                    //执行发放积分
                    signDayM.givePoint(uid,point).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.gifts) != -1){
                    //执行发放礼品卷
                    signDayM.giveGiftToCart(uid,level).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.cash) != -1){
                    var cash = parseInt(action.cash);
                    //执行发放现金
                    signDayM.giveCash(uid,cash).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else{
                    //其他特殊情况
                    callback(null, {code:0,msg:data.msg});
                }
            }],
            updateGiveFlow:['giveGift',function(callback,results){
                var isReceived = 0 ;
                if(results.giveGift.code === 0){
                    isReceived = 1 ;
                }
                signDayM.updateGive(uid,level,isReceived).then(function(data){
                    //console.log('更新礼物流水表成功');
                    callback(null, data);
                })
            }]
        },function(err,results){
            if(err){
                //console.log(err)
                promise.reject(err);
            }else{
                promise.resolve(results.updateGiveFlow);
            }
        });
        return promise;
    },

    signDayAction:function(uid){
        var promise = new AV.Promise();
        async.waterfall([
            function(callback){
                signDayM.setSignDayAction(uid,new Date()).then(function(result){
                    //console.log("签到成功");
                    callback(null, {code:0,results:result});
                }).catch(function(err){
                    callback(null,  {code:0,results:''});
                });
            },
            function(signSuccess,callback){
                if(signSuccess.code === 0){
                    //signDayM.givePoint(uid,2).then(function(res){
                    //    callback(null,signSuccess);
                    //}).catch(function(err){
                    //    callback(err);
                    //});
                    //去除赠送积分的流程 @03191455 frank
                    callback(null,signSuccess);
                }else{
                    callback({code:-1,msg:'签到失败了'});
                }
            },
            function(signSuccess,callback){
                //获取当前累计签到天数
                signDayM.getAllSignDays(uid).then(function(data){
                    //console.log("获取累计签到天数成功");
                    if(data){
                        callback(null, data.get('AllSignDays'));
                    }else{
                        callback(null, 0);
                    }  
                }).catch(function(){
                    callback(null, 0);
                });
            },
            function(allSignDays,callback){
                var level = '';
                if(allSignDays>=90){
                    //当累计签到大于90天时执行判断是否连续
                    //首先整理出签到详情
                    signDayM.getAllSignDaysDetail(uid).then(function(data){
                        if(data.code===0){
                            var now = new Date();
                            var days = singDays.getDays(data.signMonth,{year:parseInt(now.getFullYear()),month:parseInt(now.getMonth()+1),day:parseInt(now.getDate())});
                            if(days === 90){
                                level = 'D90' ;
                            }else if(days === 180){
                                level = 'D180'
                            }
                            callback(null, {level:level,allSignDays:allSignDays});

                        }
                    }).catch(function(err){
                        console.log(err);
                        callback(null, {level:level,allSignDays:allSignDays});
                    });

                }else{
                    callback(null, {level:level,allSignDays:allSignDays});
                }
            },
            function(allsigndays,callback){
                //获取奖品等级
                if(allsigndays.level === ''){
                    //不满足连续签到
                    signDayM.getGiftLevel(allsigndays.allSignDays).then(function(res){
                        //console.log("获取等级成功");
                        if(res.code === 0){
                            //console.log('累计签到，发放奖品');
                            if(res.level === 'D90' || res.level === 'D180'){
                                callback(null,{code:-1,hasGift:1,level:res.level,AllSignDays:allsigndays.allSignDays});
                            }else{
                                callback(null,{code:0,hasGift:1,level:res.level,AllSignDays:allsigndays.allSignDays});
                            }
                        }else{
                            //console.log('未累计签到，不发放奖品');
                            callback(null,{code:1,hasGift:0,level:res.level,AllSignDays:allsigndays.allSignDays});
                        }
                    });
                }else{
                    callback(null,{code:1,hasGift:1,level:allsigndays.level,AllSignDays:allsigndays.allSignDays});
                }
            },
            //发放奖品
            function(gifts,callback){
                if(gifts.code === 0){
                    var level = gifts.level ;
                    var action = global.initData.signGifts[level].action;
                    if(gifts.hasGift === 1){
                        //发放奖品
                        signDayM.isRecievedGifts(uid,level).then(function(data){
                            console.log('====getGift====isGiven========');
                            console.log(data);
                            if(data.code === 2 ){
                                //未领取过
                                callback(null,{code:0,level:level});
                            }else{
                                //已领取过，不发放
                                return callback({code:9999});
                            }
                        });
                    }else{
                        //签到成功，不发放奖品
                        //console.log('签到成功，不发放奖品');
                        callback(null,{code:-1,level:level});
                    }
                }else{
                    callback(null, gifts);
                }
            },
            function(res,callback){

                if(res.code != 0){
                    console.log('====getGift====giveGift========');
                    return callback({code:9999});
                }
                if(parseInt(action.limitMoney)!=-1){
                    //满减，获取cutMoney，执行满减方法
                    var limitMoney = parseInt(action.limitMoney) ;
                    var cutMoney = parseInt(action.cutMoney);
                    signDayM.giveMJ(uid,limitMoney,cutMoney).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.realMoney) != -1){
                    var realMoney = parseInt(action.realMoney);
                    //执行优惠劵
                    signDayM.giveCoupons(uid,realMoney).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.point) != -1){
                    var point = parseInt(action.point);
                    //执行发放积分
                    signDayM.givePoint(uid,point).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.gifts) != -1){
                    //执行发放礼品卷
                    signDayM.giveGiftToCart(uid,level).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else if(parseInt(action.cash) != -1){
                    var cash = parseInt(action.cash);
                    //执行发放现金
                    signDayM.giveCash(uid,cash).then(function(data){
                        callback(null, {code:0,msg:data.msg});
                    }).catch(function(err){
                        callback(null, {code:1,msg:""});
                    });
                }else{
                    //其他特殊情况
                    callback(null, {code:0,msg:data.msg});
                }
            },
            //发放奖品
            function(gifts,callback){

                callback(null, {code:0,hasGift:gifts.hasGift,level:level,AllSignDays:gifts.AllSignDays});
            }
        ],function(err,results){
            if(err){
                //console.log(err);
                promise.reject(err);
            }else{
                promise.resolve(results);
            }
        });

        return promise;
    }
}